<template>
  <div id="app">
    <div class="layout">
      <!-- 顶部导航栏 -->
      <header class="top-navbar">
        <div class="navbar-container">
          <div class="logo-container">
            <img src="/images/logo.png" alt="有铭汽车" class="logo-img">
            <h1 class="logo">有铭汽车</h1>
          </div>
          
          <nav class="navbar-nav">
            <ul class="nav-menu">
              <li><router-link to="/" class="nav-link"><img src="/images/首页.png" alt="首页" class="nav-icon">首页</router-link></li>
              <li><router-link to="/cars" class="nav-link"><img src="/images/汽车.png" alt="汽车浏览" class="nav-icon">汽车浏览</router-link></li>
              <li><router-link to="/ai-assistant" class="nav-link"><img src="/images/AI.png" alt="AI助手" class="nav-icon">AI助手</router-link></li>
              <li><router-link to="/stats" class="nav-link"><img src="/images/统计.png" alt="数据分析" class="nav-icon">数据分析</router-link></li>
              <li><router-link to="/profile" class="nav-link"><img src="/images/个人中心.png" alt="个人中心" class="nav-icon">个人中心</router-link></li>
            </ul>
          </nav>
          
          <!-- 登录/用户信息区域 -->
          <div class="user-section">
            <div v-if="isLoggedIn" class="user-info">
              <span class="welcome-text">欢迎, {{ nickname }}</span>
              <button @click="showLogoutDialog" class="logout-btn">退出</button>
            </div>
            <router-link v-else to="/login" class="login-btn">登录</router-link>
          </div>
        </div>
      </header>

      <!-- 主内容区域 -->
      <main class="main-content">
        <router-view />
      </main>
    </div>

    <!-- 退出登录确认对话框 -->
    <ConfirmDialog
      v-model:visible="showDialog"
      title="退出登录"
      message="确定要退出登录吗？"
      confirm-text="退出"
      cancel-text="取消"
      @confirm="handleLogoutConfirm"
      @cancel="handleLogoutCancel"
      @close="handleLogoutCancel"
    />

    <!-- 登录提示对话框 -->
    <LoginRequiredDialog
      v-model:visible="showLoginDialog"
      title="需要登录"
      message="该功能需要登录后才能使用"
      confirm-text="前往登录"
      cancel-text="暂不登录"
      @confirm="handleLoginConfirm"
      @cancel="handleLoginCancel"
      @close="handleLoginCancel"
    />
  </div>
</template>

<script>
import { authAPI } from './api/auth.js'
import ConfirmDialog from './components/ConfirmDialog.vue'
import LoginRequiredDialog from './components/LoginRequiredDialog.vue'

export default {
  name: 'App',
  components: {
    ConfirmDialog,
    LoginRequiredDialog
  },
  data() {
    return {
      isLoggedIn: false,
      username: '',
      nickname: '',
      showDialog: false,
      showLoginDialog: false,
      targetRoute: ''
    }
  },
  created() {
    this.checkLoginStatus()
    // 监听登录需求事件
    window.addEventListener('login-required', this.handleLoginRequired)
    // 监听昵称更新事件
    window.addEventListener('nickname-updated', this.handleNicknameUpdated)
  },
  beforeUnmount() {
    // 移除事件监听器
    window.removeEventListener('login-required', this.handleLoginRequired)
    window.removeEventListener('nickname-updated', this.handleNicknameUpdated)
  },
  methods: {
    checkLoginStatus() {
      this.isLoggedIn = authAPI.checkLoginStatus()
      if (this.isLoggedIn) {
        this.username = localStorage.getItem('username') || ''
        this.nickname = localStorage.getItem('nickname') || this.username
      }
    },
    handleNicknameUpdated(event) {
      // 更新导航栏显示的昵称
      this.nickname = event.detail.nickname
    },
    showLogoutDialog() {
      this.showDialog = true
    },
    handleLogoutConfirm() {
      authAPI.logout()
      this.isLoggedIn = false
      this.username = ''
      this.nickname = ''
      this.showDialog = false
      // 退出登录后跳转到首页
      this.$router.push('/')
    },
    handleLogoutCancel() {
      this.showDialog = false
    },
    // 处理登录需求事件
    handleLoginRequired(event) {
      this.targetRoute = event.detail.targetRoute
      this.showLoginDialog = true
    },
    // 处理登录确认
    handleLoginConfirm() {
      this.showLoginDialog = false
      // 跳转到登录页面
      this.$router.push('/login')
    },
    // 处理登录取消
    handleLoginCancel() {
      this.showLoginDialog = false
      // 保留在当前页面
    }
  },
  watch: {
    '$route'() {
      this.checkLoginStatus()
    }
  }
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#app {
  min-height: 100vh;
  background-color: white;
}

.layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* 顶部导航栏样式 */
.top-navbar {
  background: linear-gradient(135deg, #2E7D32 0%, #4CAF50 50%, #66BB6A 100%);
  color: white;
  box-shadow: 0 4px 20px rgba(46, 125, 50, 0.3);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.navbar-container {
  display: flex;
  align-items: center;
  padding: 0 2.5rem;
  height: 80px;
  max-width: 1400px;
  margin: 0 auto;
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-right: 3rem;
  transition: transform 0.3s ease;
}

.logo-container:hover {
  transform: scale(1.02);
}

.logo-img {
  width: 45px;
  height: 45px;
  object-fit: contain;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

.logo {
  font-size: 1.8rem;
  font-weight: 700;
  color: white;
  margin: 0;
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
  letter-spacing: 0.5px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.navbar-nav {
  flex: 1;
}

.nav-menu {
  list-style: none;
  display: flex;
  height: 100%;
  gap: 0.5rem;
}

.nav-link {
  display: flex;
  align-items: center;
  padding: 0 1.5rem;
  color: rgba(255, 255, 255, 0.9);
  text-decoration: none;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 8px;
  font-size: 1.1rem;
  height: 48px;
  font-weight: 500;
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
  position: relative;
  overflow: hidden;
}

.nav-link::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.6s ease;
}

.nav-link:hover::before {
  left: 100%;
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.15);
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.nav-link.router-link-active {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  font-weight: 600;
}

.nav-icon {
  width: 22px;
  height: 22px;
  margin-right: 0.8rem;
  object-fit: contain;
  filter: brightness(0) invert(1);
  transition: transform 0.3s ease;
}

.nav-link:hover .nav-icon {
  transform: scale(1.1);
}

/* 用户信息区域 */
.user-section {
  display: flex;
  align-items: center;
  margin-left: 2rem;
}

.login-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.8rem 1.8rem;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.25), rgba(255, 255, 255, 0.15));
  color: white;
  text-decoration: none;
  border-radius: 10px;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  font-size: 1rem;
  cursor: pointer;
  font-weight: 500;
  backdrop-filter: blur(10px);
  position: relative;
  overflow: hidden;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.6s ease;
}

.login-btn:hover::before {
  left: 100%;
}

.login-btn:hover {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0.25));
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(255, 255, 255, 0.2);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.welcome-text {
  color: rgba(255, 255, 255, 0.95);
  font-size: 1rem;
  font-weight: 500;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.logout-btn {
  padding: 0.6rem 1.2rem;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.15), rgba(255, 255, 255, 0.1));
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  font-weight: 500;
  backdrop-filter: blur(10px);
  position: relative;
  overflow: hidden;
}

.logout-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.6s ease;
}

.logout-btn:hover::before {
  left: 100%;
}

.logout-btn:hover {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.25), rgba(255, 255, 255, 0.15));
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

/* 主内容区域 */
.main-content {
  flex: 1;
  background-color: white;
  padding: 2rem;
  overflow-y: auto;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .navbar-container {
    padding: 0 2rem;
  }
  
  .nav-link {
    padding: 0 1.2rem;
    font-size: 1rem;
  }
  
  .logo {
    font-size: 1.6rem;
  }
}

@media (max-width: 768px) {
  .navbar-container {
    padding: 0 1.5rem;
    flex-wrap: wrap;
    height: auto;
    min-height: 70px;
    gap: 1rem;
  }
  
  .logo-container {
    margin-right: auto;
  }
  
  .logo {
    font-size: 1.4rem;
  }
  
  .logo-img {
    width: 35px;
    height: 35px;
  }
  
  .navbar-nav {
    order: 3;
    width: 100%;
    margin-top: 0.5rem;
  }
  
  .nav-menu {
    width: 100%;
    overflow-x: auto;
    margin: 0;
    padding: 0.5rem 0;
    justify-content: space-between;
    gap: 0.2rem;
  }
  
  .nav-link {
    padding: 0.6rem 1rem;
    font-size: 0.95rem;
    white-space: nowrap;
    border-radius: 6px;
    height: 44px;
    flex: 1;
    min-width: auto;
    justify-content: center;
  }
  
  .nav-icon {
    width: 18px;
    height: 18px;
    margin-right: 0.4rem;
  }
  
  .user-section {
    margin-left: 0;
    order: 2;
  }
  
  .user-info {
    gap: 0.8rem;
  }
  
  .welcome-text {
    font-size: 0.9rem;
  }
  
  .logout-btn {
    padding: 0.5rem 1rem;
    font-size: 0.8rem;
  }
  
  .login-btn {
    padding: 0.6rem 1.2rem;
    font-size: 0.9rem;
  }
  
  .main-content {
    padding: 1.5rem;
  }
}

@media (max-width: 480px) {
  .navbar-container {
    padding: 0 1rem;
    min-height: 65px;
  }
  
  .logo-container {
    gap: 0.6rem;
  }
  
  .logo {
    font-size: 1.2rem;
  }
  
  .logo-img {
    width: 30px;
    height: 30px;
  }
  
  .nav-menu {
    gap: 0.1rem;
  }
  
  .nav-link {
    padding: 0.5rem 0.8rem;
    font-size: 0.85rem;
    height: 40px;
  }
  
  .nav-icon {
    width: 16px;
    height: 16px;
    margin-right: 0.3rem;
  }
  
  .welcome-text {
    display: none;
  }
  
  .logout-btn {
    padding: 0.4rem 0.8rem;
    font-size: 0.75rem;
  }
  
  .login-btn {
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
  }
  
  .main-content {
    padding: 1rem;
  }
}

@media (max-width: 360px) {
  .nav-link {
    padding: 0.4rem 0.6rem;
    font-size: 0.8rem;
  }
  
  .nav-icon {
    display: none;
  }
  
  .logo {
    font-size: 1.1rem;
  }
}
</style>